<?php

/**
 * This is the model class for table "{{merchants_settled}}".
 * 商家入注Model
 */
class MerchantsSettledModel extends BaseModel
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{merchants_settled}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('CompanyName, RegistrationNo, StartTime, EndTime, Location, ScopeBusiness, FullName, Address, MobilePhone, FaxNumber, CompanyUrl, BrandAgent, TaobaoUrl, BusinessLicenseImg, OrganizationImg, TaxRegistrationImg, PositiveIDImg, ReverseIDImg, HeadName, HeadPosition, HeadCardID, HeadMobilePhone, HeadQQ, HeadEmail', 'required'),
			array('RegisteredCapital, UserID, AddTime, Status', 'numerical', 'integerOnly'=>true),
			array('MobilePhone, HeadMobilePhone', 'match', 'pattern'=>'/^1[0-9]{10}$/', 'message'=>'{attribute}必须为1开头的11位纯数字'),//验证手机号码			
			array('UserID', 'checkUnique', 'on'=>'insert', 'message' => '你已经提交过了'),//用户ID必须是唯一的
			array('HeadEmail', 'email'),//验证邮箱
			array('HeadEmail', 'checkLogin'),//验证邮箱
			array('FaxNumber', 'length', 'max'=>20),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('MerchantsSettledID, CompanyName, RegistrationNo, StartTime, EndTime, Location, RegisteredCapital, ScopeBusiness, FullName, Address, MobilePhone, FaxNumber, CompanyUrl, BrandAgent, TaobaoUrl, BusinessLicenseImg, OrganizationImg, TaxRegistrationImg, PositiveIDImg, ReverseIDImg, HeadName, HeadPosition, HeadCardID, HeadMobilePhone, HeadQQ, HeadEmail, UserID, AddTime, Status', 'safe', 'on'=>'search'),
			array('CompanyName, RegistrationNo, StartTime, EndTime, Location, RegisteredCapital, ScopeBusiness, FullName, Address, MobilePhone, FaxNumber, CompanyUrl, BrandAgent, TaobaoUrl, BusinessLicenseImg, OrganizationImg, TaxRegistrationImg, PositiveIDImg, ReverseIDImg, HeadName, HeadPosition, HeadCardID, HeadMobilePhone, HeadQQ, HeadEmail, UserID, AddTime, Status', 'safe', 'on'=>'admin'),
		);
	}

	public function checkUnique(){
		$intUid = empty($this->UserID) ? yii::app()->user->id :  $this->UserID;
		$arrData = self::model()->find(array('condition' => 'UserID = :UserID AND Status!=2', 'params' => array('UserID' => $intUid)));
		if($arrData){
			if($arrData == 0){
				$strHtml = '已经提交过但未审核';
			}else{
				$strHtml = '已经提交过且已经审核通过';
			}
			$this->addError('UserID', $strHtml);
			return false;
		}
	}

	public function checkLogin(){
		if(!yii::app()->user->isLogin()){
			$this->addError('', '你还未登录');
			return false;
		}
		if(yii::app()->user->Type == 0){
			$this->addError('', '必须商家用户才能添加');
			return false;
		}
	}


	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'MerchantsSettledID' => 'ID',
			'CompanyName' => '公司名称',
			'RegistrationNo' => '营业执照注册号',
			'StartTime' => '营业执照有效期开始时间',
			'EndTime' => '营业执照有效期结束时间',
			'Location' => '营业执照所在地',
			'RegisteredCapital' => '注册资金',
			'ScopeBusiness' => '经营范围',
			'FullName' => '法人姓名',
			'Address' => '联系地址',
			'MobilePhone' => '法人电话',
			'FaxNumber' => '传真',
			'CompanyUrl' => '公司网址',
			'BrandAgent' => '旗下母婴品牌/代理',
			'TaobaoUrl' => '淘宝/天猫店网址',
			'BusinessLicenseImg' => '营业执照扫描件',
			'OrganizationImg' => '组织机构代码证扫描件',
			'TaxRegistrationImg' => '税务登记证件扫描件(国税或者地税)',
			'PositiveIDImg' => '法人身份证正面',
			'ReverseIDImg' => '法人身份证反面',
			'HeadName' => '姓名',
			'HeadPosition' => '职位',
			'HeadCardID' => '身份证号码',
			'HeadMobilePhone' => '手机',
			'HeadQQ' => 'QQ',
			'HeadEmail' => '常用邮箱',
			'UserID' => '用户ID',
			'AddTime' => '添加时间',
			'Status' => '审核状态',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('MerchantsSettledID',$this->MerchantsSettledID);
		$criteria->compare('CompanyName',$this->CompanyName,true);
		$criteria->compare('RegistrationNo',$this->RegistrationNo,true);
		$criteria->compare('StartTime',$this->StartTime);
		$criteria->compare('EndTime',$this->EndTime);
		$criteria->compare('Location',$this->Location,true);
		$criteria->compare('RegisteredCapital',$this->RegisteredCapital);
		$criteria->compare('ScopeBusiness',$this->ScopeBusiness,true);
		$criteria->compare('FullName',$this->FullName,true);
		$criteria->compare('Address',$this->Address,true);
		$criteria->compare('MobilePhone',$this->MobilePhone,true);
		$criteria->compare('FaxNumber',$this->FaxNumber,true);
		$criteria->compare('CompanyUrl',$this->CompanyUrl,true);
		$criteria->compare('BrandAgent',$this->BrandAgent,true);
		$criteria->compare('TaobaoUrl',$this->TaobaoUrl,true);
		$criteria->compare('BusinessLicenseImg',$this->BusinessLicenseImg,true);
		$criteria->compare('OrganizationImg',$this->OrganizationImg,true);
		$criteria->compare('TaxRegistrationImg',$this->TaxRegistrationImg,true);
		$criteria->compare('PositiveIDImg',$this->PositiveIDImg,true);
		$criteria->compare('ReverseIDImg',$this->ReverseIDImg,true);
		$criteria->compare('HeadName',$this->HeadName,true);
		$criteria->compare('HeadPosition',$this->HeadPosition,true);
		$criteria->compare('HeadCardID',$this->HeadCardID,true);
		$criteria->compare('HeadMobilePhone',$this->HeadMobilePhone,true);
		$criteria->compare('HeadQQ',$this->HeadQQ,true);
		$criteria->compare('HeadEmail',$this->HeadEmail,true);
		$criteria->compare('UserID',$this->UserID);
		$criteria->compare('AddTime',$this->AddTime);
		$criteria->compare('Status',$this->Status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function createSearchCriteria()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('MerchantsSettledID',$this->MerchantsSettledID);
		$criteria->compare('CompanyName',$this->CompanyName,true);
		$criteria->compare('RegistrationNo',$this->RegistrationNo,true);
		$criteria->compare('StartTime',$this->StartTime);
		$criteria->compare('EndTime',$this->EndTime);
		$criteria->compare('Location',$this->Location,true);
		$criteria->compare('RegisteredCapital',$this->RegisteredCapital);
		$criteria->compare('ScopeBusiness',$this->ScopeBusiness,true);
		$criteria->compare('FullName',$this->FullName,true);
		$criteria->compare('Address',$this->Address,true);
		$criteria->compare('MobilePhone',$this->MobilePhone,true);
		$criteria->compare('FaxNumber',$this->FaxNumber,true);
		$criteria->compare('CompanyUrl',$this->CompanyUrl,true);
		$criteria->compare('BrandAgent',$this->BrandAgent,true);
		$criteria->compare('TaobaoUrl',$this->TaobaoUrl,true);
		$criteria->compare('BusinessLicenseImg',$this->BusinessLicenseImg,true);
		$criteria->compare('OrganizationImg',$this->OrganizationImg,true);
		$criteria->compare('TaxRegistrationImg',$this->TaxRegistrationImg,true);
		$criteria->compare('PositiveIDImg',$this->PositiveIDImg,true);
		$criteria->compare('ReverseIDImg',$this->ReverseIDImg,true);
		$criteria->compare('HeadName',$this->HeadName,true);
		$criteria->compare('HeadPosition',$this->HeadPosition,true);
		$criteria->compare('HeadCardID',$this->HeadCardID,true);
		$criteria->compare('HeadMobilePhone',$this->HeadMobilePhone,true);
		$criteria->compare('HeadQQ',$this->HeadQQ,true);
		$criteria->compare('HeadEmail',$this->HeadEmail,true);
		$criteria->compare('UserID',$this->UserID);
		$criteria->compare('AddTime',$this->AddTime);
		$criteria->compare('Status',$this->Status);

		return $criteria;
	}




	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return MerchantsSettledModel the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function beforeSave(){
		if ($this->isNewRecord){
			$this->AddTime = time();
			$this->Status = 0;
			if(!$this->UserID){
				$this->UserID = yii::app()->user->id;
			}
		}
		if(!is_numeric($this->StartTime)){
			$this->StartTime = strtotime($this->StartTime);
		}
		if(!is_numeric($this->EndTime)){
			$this->EndTime = strtotime($this->EndTime);
		}
		//$config = ConfigModel::model()->getConfig();
		return true;
	}

	public static function getStatusHtml($intData = ''){
		$arrData = array(
			0 => '未审核',
			1 => '审核通过',
			2 => '审核不通过',
			3 => '取消',
 		);
		if($intData === ''){
			return $arrData;
		}else{
			return $arrData[$intData];
		}
	}
}
